package com.sihan.framework.etm.controller.business;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.CacheOperatorInfo;
import com.sihan.framework.etm.common.CommonBiz;
import com.sihan.framework.etm.common.PageRequest;
import com.sihan.framework.etm.common.util.DateUtils;
import com.sihan.framework.etm.dto.business.BorrowToolTaskDTO;
import com.sihan.framework.etm.dto.business.ToolBorrowReturnRecordDTO;
import com.sihan.framework.etm.entity.business.ToolUseTask;
import com.sihan.framework.etm.enums.CacheKeyName;
import com.sihan.framework.etm.enums.MqttSendCommandFunc;
import com.sihan.framework.etm.mapper.business.ToolUseTaskMapper;
import com.sihan.framework.etm.service.business.BorrowService;
import com.sihan.framework.etm.vo.business.BorrowDetailVO;
import com.sihan.framework.etm.vo.business.BorrowPlanVO;
import com.sihan.framework.etm.vo.business.CacheOcrModel;
import com.sihan.framework.etm.vo.business.ReturnDetailVO;
import com.sihan.framework.etm.vo.business.ToolBorrowReturnRecordVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "工器具领用与归还管理")
@RestController
@RequestMapping("/bs/v0/borrow")
public class ToolUseController {

	@Autowired
	private BorrowService borrowService;
	
	@Autowired
	private CommonBiz commonBiz;
	
	@Autowired
	private ToolUseTaskMapper toolUseTaskMapper;
	
	@ApiOperation(value = "验证是否有领用权限", notes = "验证是否有领用权限")
	@GetMapping("/permission/has_borrow")
	@BusinessLog("验证是否有领用权限")
	@RequiresPermissions("bs:manager:tool:borrow")
	public ApiResult<?> validateHasBorrowPermission(){
		return ApiResult.success();
	}
	
	@ApiOperation(value = "验证是否有归还权限", notes = "验证是否有归还权限")
	@GetMapping("/permission/has_return")
	@BusinessLog("验证是否有归还权限")
	@RequiresPermissions("bs:manager:tool:return")
	public ApiResult<?> validateHasReturnPermission(){
		commonBiz.sendCommandToDevice(MqttSendCommandFunc.COMMAND_2000.getValue());
		return ApiResult.success();
	}
	
	@ApiOperation(value = "获取识别的任务名称", notes = "获取识别的任务名称")
	@GetMapping("/task_name")
	@BusinessLog("获取识别的任务名称")
	@RequiresPermissions("bs:manager:tool:borrow")
	public ApiResult<?> selectTaskName(){
		BorrowToolTaskDTO dto = new BorrowToolTaskDTO();
		CacheOcrModel cache = (CacheOcrModel)CacheOperatorInfo.getMapValue(
				CacheKeyName.CACHE_BORROW_OCR_SCAN.getValue(), null);
		String taskId = (String)CacheOperatorInfo.getMapValue(
				CacheKeyName.CACHE_BORROW_TASK_ID.getValue(), null);
		BorrowToolTaskDTO toolTask = (BorrowToolTaskDTO)CacheOperatorInfo.getMapValue(
				CacheKeyName.CACHE_BORROW_REASON.getValue(), null);
		if(cache != null) {
			dto.setTaskName(cache.getOcrName());
			dto.setTaskCode(cache.getOcrCode());
			dto.setStartTime(cache.getStartTime());
			dto.setEndTime(cache.getEndTime());
		}
		
		if(!StringUtils.isBlank(taskId)) {
			ToolUseTask task = toolUseTaskMapper.selectById(taskId);
			dto.setTaskName(task.getName());
			dto.setStartTime(DateUtils.getInstance().getDateStr(task.getStartTime(), DateUtils.getInstance().SDF));
			dto.setEndTime(DateUtils.getInstance().getDateStr(task.getEndTime(), DateUtils.getInstance().SDF));
		}
		
		if(toolTask != null) {
			dto.setTaskName(toolTask.getTaskName());
			dto.setTaskCode(toolTask.getTaskCode());
			dto.setStartTime(toolTask.getStartTime());
			dto.setEndTime(toolTask.getEndTime());
		}
		return ApiResult.success(dto);
	}
	
	@ApiOperation(value = "查询计划", notes = "查询计划", response = BorrowDetailVO.class)
	@GetMapping("/plan")
	@BusinessLog("查询计划")
	@RequiresPermissions("bs:manager:tool:borrow")
	public ApiResult<?> selectPlanByUserId(){
		return ApiResult.success(borrowService.selectLastNoDoneBorrowPlan());
	}
	
	@ApiOperation(value = "领用明细列表", notes = "领用明细列表", response = BorrowDetailVO.class)
	@GetMapping("/details")
	@BusinessLog("领用明细列表")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query")
	  })
	@RequiresPermissions("bs:manager:tool:borrow")
	public ApiResult<?> listBorrowDetail(PageRequest dto){
		return ApiResult.success(borrowService.selectBorrowDetailByMasterId(dto));
	}
	
	@ApiOperation(value = "确认领取清单", notes = "确认领取清单", response = ApiResult.class)
	@GetMapping("/confirm")
	@BusinessLog("确认领取清单")
	@RequiresPermissions("bs:manager:tool:borrow")
	public ApiResult<?> confirmBorrow(Boolean confirm, String taskName, String startTime, String endTime){
		return borrowService.confirmBorrow(confirm, taskName, startTime, endTime);
	}
	
	@ApiOperation(value = "查询归还统计", notes = "查询归还统计", response = BorrowPlanVO.class)
	@GetMapping("/return/statistics")
	@BusinessLog("查询归还统计")
	@RequiresPermissions("bs:manager:tool:return")
	public ApiResult<?> selectReturnStatisticsByUserId(){
		return ApiResult.success(borrowService.selectReturnStatisticsByUserId());
	}
	
	@ApiOperation(value = "归还明细列表", notes = "归还明细列表", response = ReturnDetailVO.class)
	@GetMapping("/return/details")
	@BusinessLog("归还明细列表")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query")
	  })
	@RequiresPermissions("bs:manager:tool:return")
	public ApiResult<?> listReturnDetail(PageRequest dto){
		//ApiResult result = new ApiResult(true,"请前往领取工器具，领取完成后请回来确认清单");
		//result.setResult(borrowService.selectBorrowDetailByMasterId(dto));
		return ApiResult.success(borrowService.selectReturnDetailByMasterId(dto));
	}
	
	@ApiOperation(value = "确认归还清单", notes = "确认归还清单", response = ApiResult.class)
	@GetMapping("/return/confirm")
	@BusinessLog("确认归还清单")
	@RequiresPermissions("bs:manager:tool:return")
	public ApiResult<?> confirmReturn(){
		return borrowService.confirmRetrun();
	}
	
	@ApiOperation(value = "领用归还台账", notes = "领用归还台账", response = ToolBorrowReturnRecordVO.class)
	@GetMapping("/summary")
	@BusinessLog("领用归还台账")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "toolCode", value = "工器具编号", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "deviceCode", value = "柜子编号", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "typeModel", value = "类型和型号", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryStartTime", value = "查询开始时间", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
	  })
	@RequiresPermissions("bs:manager:tool:use:standing")
	public ApiResult<?> selectToolBorrowReturnRecord(ToolBorrowReturnRecordDTO dto){
		return ApiResult.success(borrowService.selectToolBorrowReturnRecord(dto));
	}
	
	@ApiOperation(value = "异常上报与取消", notes = "异常上报与取消", response = ApiResult.class)
	@GetMapping("/return/exception")
	@BusinessLog("异常上报与取消")
	public ApiResult<?> toolExceptionRecord(
			@ApiParam(name = "toolId", value = "清单Id", required = true) @RequestParam String toolId){
		return borrowService.toolExceptionRecord(toolId);
	}
	
	@ApiOperation("领用计划图片拍照")
	@PostMapping("/plan/book/img")
	@BusinessLog("领用计划图片拍照")
	@RequiresAuthentication
	public ApiResult<?> planBookImg(String img) {
		return borrowService.planBookImg(img);
	}
}
